package controllers;

import java.util.ArrayList;

import dataobjects.AlbumTemplate;
import dataobjects.CardInstance;
import dataobjects.Invite;
import dataobjects.User;
import dbaccessors.DBAccessor;
import api.API;

public class UIController implements API {

	private static UIController instance = null;


	public UIController() {
	}

	public static UIController getInstance() {

		if (instance == null) {
			instance = new UIController();
		}
		return instance;
	}

	// methods

	@Override
	public boolean register(String username, String name, String mail,
			String password) {
		System.out.println("register");
		return UserController.getInstance().register(username, name, mail,
				password);
	}

	@Override
	public boolean login(String username, String password) {
		return UserController.getInstance().login(username, password);
	}

	public boolean signout(String username) {
		System.out.println("logout: " + username);
		return UserController.getInstance().logout(username);

	}

	@Override
	public ArrayList<CardInstance> enterPincode(String username, String pinCode) {
		System.out.println("EnterPincode: " + username + ", " + pinCode);
		//ArrayList<CardInstance> ans = new ArrayList<CardInstance>();
		String pincode = PinCodeController.getInstance().decode(pinCode);
		ArrayList<CardInstance> iids = DBAccessor.getInstance().getCardsOfPincode(
				pincode,username);
		
		return iids;
	}

	@Override
	public String getNotifications(String username) {

		return DBAccessor.getInstance().getNotifications(username);
	}

	@Override
	public User getUserDetails(String username) {
		return UserController.getInstance().getUser(username);
	}

	@Override
	public ArrayList<AlbumTemplate> getActiveAlbums() {
		return AlbumController.getInstance().getActiveAlbums();
	}

	@Override
	public ArrayList<Integer> pasteCard(String username, int cardInstanceID,
			int albumID) {
		return AlbumController.getInstance().pasteCard(cardInstanceID,
				username, albumID);
	}

	@Override
	public ArrayList<User> findMatchingUsers(String username) {
		return UserController.getInstance().findMatchingUsers(username);
	}

	@Override
	public boolean createTradeInvite(String requestingUser,
			String acceptingUser, int givesID, int getsID) {
		return UserController.getInstance().createTradeInvite(requestingUser,
				acceptingUser, givesID, getsID);
	}

	public boolean addToTradeList(int cardID, String username) {
		return CardController.getInstance().addToTradeList(cardID, username);
	}

	public boolean removeFromTradeList(int cardID, String username) {
		return CardController.getInstance().removeFromTradeList(cardID,
				username);
	}

	public boolean addToWishList(int cardID, String username) {
		return CardController.getInstance().addToWishList(cardID, username);
	}

	public boolean removeFromWishList(int cardID, String username) {
		return CardController.getInstance()
				.removeFromWishList(cardID, username);
	}

	@Override
	public ArrayList<Invite> getInvites(String username) {
		System.out.println("UIController: getInvites for " + username);
		return UserController.getInstance().getInvites(username);
	}

	@Override
	public Invite confirmInvite(String askUsername, String recievingUsername,
			int wantInstanceID, int giveInstanceID, String type) {
		System.out.println("UIController: confirmInvite ");
		return UserController.getInstance().confirmInvite(askUsername,
				recievingUsername, wantInstanceID, giveInstanceID, type);
	}

	@Override
	public boolean declineInvite(String askUsername, String recievingUsername,
			int wantInstanceID, int giveInstanceID, String type) {
		System.out.println("UIController: declineInvite ");
		return UserController.getInstance().declineInvite(askUsername,
				recievingUsername, wantInstanceID, giveInstanceID, type);
	}

	@Override
	public Invite createGameInvite(String requestingUser, String acceptingUser,
			int givesID, int getsID, String type) {
		return UserController.getInstance().createGameInvite(requestingUser,
				acceptingUser, givesID, getsID, type);
	}

	@Override
	public boolean cancelGameInvite(String askingUsername,
			String receivingUsername, int giveCardID, int wantCardID,
			String type) {
		return UserController.getInstance().cancelGameInvite(askingUsername,
				receivingUsername, giveCardID, wantCardID, type);
	}

	@Override
	public String makeTurnRPC(String username, String turn, String gameName) {
		return GameController.getInstance().makeTurn(username, turn, gameName);
	}

}
